package com.wushijia.designpatterns.observer.good;

import com.wushijia.designpatterns.observer.bug.LotteryResult;
import com.wushijia.designpatterns.observer.good.event.EventManager;
import com.wushijia.designpatterns.observer.good.event.EventManager.EventType;
import com.wushijia.designpatterns.observer.good.event.listen.MQEventListen;
import com.wushijia.designpatterns.observer.good.event.listen.MessageEventListen;

public abstract class LotteryService {

  private EventManager eventManager;

  public LotteryService() {
    eventManager = new EventManager(EventType.Message, EventType.MQ);
    eventManager.subscribe(EventType.Message, new MessageEventListen());
    eventManager.subscribe(EventType.MQ, new MQEventListen());
  }

  public LotteryResult draw(String uid) {
    LotteryResult lotteryResult = doDraw(uid);        // 需要什么通知就调用什么方法
    eventManager.notify(EventManager.EventType.MQ, lotteryResult);
    eventManager.notify(EventManager.EventType.Message, lotteryResult);
    return lotteryResult;
  }

  protected abstract LotteryResult doDraw(String uid);
}
